package com.tianlan.blog.config;

import java.util.Optional;

import org.apache.shiro.SecurityUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@Configuration
public class UserIdAuditorBean implements AuditorAware<Integer> {

	@Override
	public Optional<Integer> getCurrentAuditor() {
		String principal = (String) SecurityUtils.getSubject().getPrincipal();
		if (null != principal) {
			JSONObject userObj = JSON.parseObject(principal);
			Integer userId = userObj.getInteger("id");
			return Optional.of(userId);
		}
		return null;
	}
	
	public Integer getUserId() {
		String principal = (String) SecurityUtils.getSubject().getPrincipal();
		if (null != principal) {
			JSONObject userObj = JSON.parseObject(principal);
			Integer userId = userObj.getInteger("id");
			return userId;
		}
		return null;
	}
}
